<?php

namespace app\controllers;

use yii\data\Pagination;
use yii\db\Query;
use yii\web\Controller;


class UserController extends AdminBaseController {

     //用户数据
     public function actionIndex(){
         if ($this->post()){
             $nickName=\Yii::$app->request->post()['nickName'];
             $query=(new Query())->select('*')->from('yii_user')->where(['like','nickName',$nickName]);
         }else{
             $query=(new Query())->select('*')->from('yii_user');
         }

         $pages=new Pagination(['totalCount' =>$query->count()]);
         $pages->defaultPageSize = 10;//每页10条
         $list=$query->offset($pages->offset)->limit($pages->limit)->orderBy('create_time desc')->all();
         return $this->render('index',['list'=>$list,'pages'=>$pages]);
       }


       //删除用户
       public function actionDel(){
           $id=$this->PostId();
           $transaction = \Yii::$app->db->beginTransaction();
           try {
               \Yii::$app->db->createCommand()->delete('yii_user',array('user_id'=>$id))->execute();
               $transaction->commit();
               return json_encode(['code'=>1,'msg'=>'删除成功']);
           } catch (\Exception $e) {
               $transaction->rollBack();
               return json_encode(['code'=>0,'msg'=>'删除失败']);
           }

       }


       public function actionDelall(){
           $id=\Yii::$app->request->post()['id'];
           $arr=explode(',',$id);
           $transaction = \Yii::$app->db->beginTransaction();
           try {
               foreach ($arr as $v){
                \Yii::$app->db->createCommand()->delete('yii_user',array('user_id'=>$v))->execute();
               }
               $transaction->commit();
               return json_encode(['code'=>1,'msg'=>'删除成功']);
           } catch (\Exception $e) {
               $transaction->rollBack();
               return json_encode(['code'=>0,'msg'=>'删除失败']);
           }
       }



}
